<template>
  <el-button type="primary" @click="downloadFun" v-if="type === 'download'">{{
    btnTxt
  }}</el-button>
  <el-button type="primary" @click="importFun" v-if="type === 'import'">{{
    btnTxt
  }}</el-button>
  <el-button type="primary" @click="downloadFun" v-if="type === 'export'">{{
    btnTxt
  }}</el-button>
  <el-button type="danger" @click="addFun" v-if="type === 'add'">{{
    btnTxt
  }}</el-button>
  <el-button type="danger" @click="checkFun" v-if="type === 'delete'">{{
    btnTxt
  }}</el-button>
</template>

<script setup lang="ts">
import { downloadFile } from "@/utils/downLoad.ts";
const props = defineProps({
  txt: {
    type: String,
    default: "删除",
  },
  type: {
    type: String,
    default: "delete",
  },
  file: {
    type: Object,
    default: () => ({
      fileName: "",
      fileId: "",
    }),
  },
  params: {
    type: Object,
    default: () => ({}),
  },
  ids: {
    type: Array,
    default: () => [],
  },
  toPath: {
    type: String,
    default: ''
  },
  request: {
    type: Function,
    default() {
      return ''
    }
  }
});
const router = useRouter()
const emit = defineEmits(['add'])
const btnTxt = computed(() => {
  let text = "";
  if (props.txt) {
    text = props.txt;
  } else {
    switch (props.type) {
      case "delete":
        text = "删除";
        break;
      case "add":
        text = "新增";
        break;
      case "download":
        text = "模板下载";
        break;
      case "import":
        text = "导入";
        break;
      default: ""
        break;
    }
  }
  return text 
});
const addFun = () => {
  if(props.toPath) {
    router.push({
      path: props.toPath,
      query: {
        ...props.params
      }
    })
  }
}
const checkFun = () => {
  if(props.ids?.length == 0) {
    ElMessage.error('请选择操作数据')
    return
  }
  ElMessageBox.confirm("请确认是否删除该数据?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      ElMessage({
        type: "success",
        message: "Delete completed",
      });
    })
    .catch(() => {});
};
const downloadFun = () => {
  downloadFile();
};
// 导入
const importExcel = async (file: any) => {
  let formData = new FormData(); //声明一个FormDate对象
  formData.append("file", file.raw);    //把文件信息放入对象中
  //调用后台导入的接口
  props.request(formData).then(res => {
      if (res && res.code === 0) {
          ElMessage.success('导入成功！')
      }else {
          ElMessage.error('导入失败!请稍后重试')
      }
  })
}
</script>
